package qq.com;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.List;

public class QQServer {

	public static void start() throws IOException, ClassNotFoundException {
		// 1.鍚姩涓�涓猄erverSocket鏈嶅姟锛岀洃鍚�9999绔彛
		ServerSocket ss = new ServerSocket(9999);
		while (true) {
			// 2.鎺ユ敹瀹㈡埛绔繛鎺�
			Socket socket = ss.accept();
			// 3.鑾峰彇socket鍒癹vm鐨勮緭鍏ユ祦
			InputStream is = socket.getInputStream();
			// 4.灏佽涓�涓璞＄殑杈撳叆娴�
			ObjectInputStream ois = new ObjectInputStream(is);
			// 5.璇诲彇璇锋眰鍙傛暟瀵硅薄
			Object readObject = ois.readObject();
			QQRequest request = (QQRequest) readObject;
			//System.out.println("鐢ㄦ埛鍚嶏細" + request.getUsername() + "====瀵嗙爜锛�" + request.getPassword());
			// 6.鍐欏叆鏈湴鏂囦欢
			QQResponse response = operator(request);
			// 7.杩斿洖瀹㈡埛绔竴涓搷搴旂粨鏋�
			// 8.鑾峰彇socket杈撳嚭娴�
			OutputStream outputStream = socket.getOutputStream();
			// 9.灏唖ocket鍘熺敓杈撳嚭娴佸皝瑁呬负瀵硅薄杈撳嚭娴佹柟渚跨洿鎺ュ啓瀵硅薄
			ObjectOutputStream oos = new ObjectOutputStream(outputStream);
			// 10.灏嗘敞鍐屽搷搴旂粨鏋滈�氳繃socket浼犵粰瀹㈡埛绔�
			oos.writeObject(response);
			oos.flush();
		}
	}

	private static QQResponse operator(QQRequest request) throws IOException {
		QQResponse response = new QQResponse();
		Integer operator = request.getOperator();
		if(operator == 1){//濡傛灉鏄敞鍐�
			File file = new File("F:\\QQFiles\\" + request.getUsername());
			if (file.exists()) {
				System.out.println("鐢ㄦ埛锛�" + request.getUsername() + "宸叉敞鍐岋紒");
				response.setSuccess(false);
				response.setMsg("娉ㄥ唽澶辫触锛岀敤鎴峰凡瀛樺湪");
			} else {
				file.mkdirs();//鍒涘缓鐢ㄦ埛鏂囦欢澶�(鍔犱簡S涔嬪悗鏂规硶鍙互寮哄埗鍒涘缓澶氱骇鏂囦欢澶�)
				File file1 = new File("F:\\QQFiles\\" + request.getUsername() + "\\account.txt");
				FileWriter fw = new FileWriter(file1);
				fw.write(request.getUsername() + "#" + request.getPassword());
				fw.flush();
				fw.close();
				response.setSuccess(true);
				response.setMsg("娉ㄥ唽鎴愬姛");
			}
		}else if(operator == 2){//濡傛灉鏄櫥褰�
			File file = new File("F:\\QQFiles\\" + request.getUsername() + "\\account.txt");
			if(file.exists()){
				FileReader fr = new FileReader(file);
				BufferedReader br = new BufferedReader(fr);//鍖呰瀛楃娴侊紝浣跨敤readLine
				String str = br.readLine();
				String username = str.split("#")[0];//鏍规嵁#鍙峰垏鍓插瓧绗︿覆鑾峰彇鍒扮敤鎴峰悕鍜屽瘑鐮� 渚嬪锛�315055923#abc1234
				String password = str.split("#")[1];//鏍规嵁#鍙峰垏鍓插瓧绗︿覆鑾峰彇鍒扮敤鎴峰悕鍜屽瘑鐮�
				if(username.equals(request.getUsername()) && password.equals(request.getPassword())){
					response.setSuccess(true);
					response.setMsg("鐧诲綍鎴愬姛锛岀埥锛�");
					//鐧诲綍鎴愬姛锛屽姞杞藉ソ鍙嬩俊鎭�
					loadFriendsInfo(request, response);
				}else{
					response.setSuccess(false);
					response.setMsg("鐧诲綍澶辫触锛屽笎鍙锋垨瀵嗙爜閿欒锛屼笉鐖斤紒");
				}
			}else{
				response.setSuccess(false);
				response.setMsg("鐧诲綍澶辫触锛岃鍏堟敞鍐�");
			}
		}else if(operator == 3){//娣诲姞濂藉弸
			//1.鏍￠獙鐢ㄦ埛鏄惁鎷ユ湁QQ甯愬彿
			boolean flag = false;
			flag = checkUserExists(request, flag);
			if(!flag){
				response.setSuccess(false);
				response.setMsg("浣犺娣诲姞鐨勫ソ鍙嬩笉瀛樺湪锛岃妫�鏌ヤ綘鐨勮緭鍏ユ槸鍚︽纭紒");
				return response;
			}
			//2.閿佸畾褰撳墠鐧诲綍鐢ㄦ埛鐨勫ソ鍙嬫枃浠讹紝濡傛灉璇ユ枃浠朵笉瀛樺湪锛岃鏄庢槸绗竴娆℃坊鍔犲ソ鍙嬶紝闇�瑕佸垱寤烘枃浠跺苟鍐欏叆绗竴涓ソ鍙嬩俊鎭�
			File file = new File("F:\\QQFiles\\" + request.getUsername() + "\\friends.txt");
			if(file.exists()){
				//濡傛灉瀛樺湪濂藉弸鏂囦欢锛岃鏄庡凡缁忔湁娣诲姞濂藉弸锛岃鍙栧ソ鍙嬫枃浠讹紝鑾峰彇濂藉弸鍒楄〃锛屽垽鏂綋鍓嶈娣诲姞鐨勫ソ鍙嬫槸鍚︽槸宸插瓨鍦�
				BufferedReader br = new BufferedReader(new FileReader(file));
				String s = br.readLine();
				String[] friends = s.split("#");
				//鏍￠獙濂藉弸鏄惁瀛樺湪
				boolean flag1 = false;
				flag1 = checkFriendExists(request,friends, flag1);
				if(flag1){
					response.setSuccess(false);
					response.setMsg("浣犲凡娣诲姞璇ュソ鍙嬶紒");
					return response;
				}
				br.close();//鍐欐枃浠朵箣鍓嶆妸璇诲叧鎺夛紝闃叉璇诲啓鍐茬獊瀵艰嚧绋嬪簭寮傚父
				FileWriter fw = new FileWriter(file);
				fw.write(s + "#" + request.getFriend());
				fw.flush();
				fw.close();
				response.setSuccess(true);
				response.setMsg("娣诲姞鎴愬姛");
				return response;
			}else{
				file.createNewFile();
				FileWriter fw = new FileWriter(file);
				fw.write(request.getFriend());
				fw.flush();
				fw.close();
				response.setSuccess(true);
				response.setMsg("娣诲姞鎴愬姛");
				return response;
			}
		}else if(operator == 4) {//鏌ヨ濂藉弸鍒楄〃
			loadFriendsInfo(request, response);
			response.setSuccess(true);
			response.setMsg("鍔犺浇濂藉弸鍒楄〃鎴愬姛");
		}else if(operator == 5) {//鎺ユ敹QQ娑堟伅
			File file = new File("F:\\QQFiles\\" + request.getUsername() + "\\"+ request.getFriend()+".txt");
			if(file.exists()){
				/*BufferedReader br = new BufferedReader(new FileReader(file));
				List<String> chatLog = new ArrayList<>();
				String str = "";
				while((str = br.readLine()) != null){
					chatLog.add(str);
				}
				br.close();
				chatLog.add(request.getMsg());*/
				BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
				bw.write(request.getMsg());
				bw.newLine();
				bw.flush();
				bw.close();
			}else{
				file.createNewFile();
				BufferedWriter bw = new BufferedWriter(new FileWriter(file));
				bw.write(request.getMsg());
				bw.newLine();
				bw.close();
			}
			response.setSuccess(true);
			response.setMsg("鍙戦�佽亰澶╄褰曞埌鏈嶅姟鍣ㄦ垚鍔�");
		}
		return response;
	}

	private static void loadFriendsInfo(QQRequest request, QQResponse response) throws IOException {
		File friendFile = new File("F:\\QQFiles\\" + request.getUsername() + "\\friends.txt");
		if(friendFile.exists()){
            BufferedReader br1 = new BufferedReader(new FileReader(friendFile));
            String[] friends = br1.readLine().split("#");
            response.setFriends(friends);
            br1.close();
        }
	}


	private static boolean checkFriendExists(QQRequest request, String[] friends, boolean flag1) {
		String friend = request.getFriend();
		for(String fr : friends){
			if(friend.equals(fr)){
				return true;
			}
		}
		return false;
	}

	private static boolean checkUserExists(QQRequest request, boolean flag) {
		String friend = request.getFriend();
		File file = new File("F:\\QQFiles");
		File[] files = file.listFiles();
		System.out.println("褰撳墠绯荤粺鎷ユ湁鐨勭敤鎴凤細");
		for(File f : files){
            if(friend.equals(f.getName())){
                flag = true;
                break;
            }
        }
		return flag;
	}

	private static boolean ifFriendExists(QQRequest request) {
		boolean friendExists = false;
		File dir = new File("F:\\QQFiles\\");
		File[] users = dir.listFiles();
		for(File f : users){
            if(f.getName().equals(request.getFriend())){
                friendExists = true;
            }
        }
		return friendExists;
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		start();
	}

}
